package com.demo.sbexample.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.demo.sbexample.entity.Result;
import com.demo.sbexample.entity.SysUser;
import com.demo.sbexample.service.SysUserService;
import com.demo.sbexample.utils.ResultUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /*
     * @ApiOperation注解来给API增加说明、通过@ApiImplicitParams、@ApiImplicitParam注解来给参数增加说明
     * 访问swagger生成的文档，可用如下的地址进入http://localhost:8080/swagger-ui.html
     * */
    //查询所有
    @ApiOperation(value="获取人员列表", notes="获取所有的人员列表")
    @GetMapping("/users")
    public List<SysUser> listSysUser(){
        return sysUserService.selectList(null);

    }

    @GetMapping("/users/query")
    public List<SysUser> listSysUserByCon(){
        //根据条件进行查询，姓名为‘张三’、性别为男，且年龄在18至50之间的用户记录
        return sysUserService.selectList(new EntityWrapper<SysUser>().eq("userName","张三").
                eq("sex","F").between("age",19,40));
    }

    //通过sql语句返回整数
    @GetMapping("/users/query1")
    public Integer ListSysUserSql(){
        return sysUserService.selectUserSql();
    }

    //通过xml sql语句查询
    @GetMapping("/users/query2")
    public List<SysUser> ListSysUserSql2(){
        return sysUserService.selectUserSql2();
    }

    //通过@Select()注解查询
    @GetMapping("/users/query3")
    public List<SysUser> ListSysUserSql3(){
        return sysUserService.selectUserSql3();
    }

    //按id查询
    @ApiOperation(value="获取人员的详细信息", notes="根据url的id来获取人员的详细信息")
    @ApiImplicitParam(name = "id", value = "人员ID", required = true, dataType = "Long")
    @GetMapping("/users/{id}")
    public SysUser findSysUserById(@PathVariable("id") Integer id){
        return sysUserService.selectById(id);
    }

    @ApiOperation(value = "添加用户信息",notes="用户信息")
    @ApiImplicitParam(name = "user",value = "用户对象",required = true,dataType = "SysUser")
    @PostMapping("/users")//使用Postman 提交post请求时，header不需要设置
    public Result<SysUser> addUser(@Valid SysUser user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {//验证失败
            return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
        }


        boolean result = sysUserService.insert(user);

        if(result){
            return ResultUtil.success(user);

        }
        return ResultUtil.error(1,"保存失败");
    }

    @PutMapping("/users/{id}")//注意在POSTMAN提交为PUT请求时，类型应选x-www-form-urlencoded,因为请求路径中也带有了参数
    public Result<SysUser> updateUser(@Valid SysUser user,BindingResult bindingResult,@PathVariable("id") Integer id){
        if (bindingResult.hasErrors()) {//验证失败
            return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
        }

        user.setUserId(id);
        boolean result = sysUserService.updateById(user);


        if(result){
            return ResultUtil.success(user);

        }
        return ResultUtil.error(1,"保存失败");

    }

    @DeleteMapping("/users/{id}")
    public boolean deleteUser(@PathVariable Integer id){

        return sysUserService.deleteById(id);


    }



    /*@ApiOperation(value="获取人员的详细信息", notes="根据url的id来获取人员的详细信息")
    * @ApiImplicitParams({
            @ApiImplicitParam(name = "person_no", value = "人员no", required = true, dataType = "String"),
            @ApiImplicitParam(name = "person_name", value = "人员名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sex", value = "性别", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "age", value = "年龄", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "birthday", value = "生日", required = false, dataType = "String")
    })
    * */
    @GetMapping("/users/add")
    public boolean addSysUser(){
        SysUser user = new SysUser();
        user.setUserNo("2001");
        user.setUserName("爱好");
        user.setSex("F");
        user.setAge(18);
        user.setBirthday(new Date());
        return sysUserService.insert(user);
    }

    @GetMapping("/users/update")
    public boolean updateSysUser(){
        SysUser user = new SysUser();
        user.setUserId(2);
        user.setUserNo("2011");
        user.setUserName("爱好222");
        user.setSex("F");
        user.setAge(188);
        user.setBirthday(new Date());

        return sysUserService.updateById(user);
    }

    @GetMapping("/users/del")
    public boolean delSysUser(){

        return sysUserService.deleteById(1);
    }


}
